package com.jshongli.myapplication.common.callback;

import android.content.Context;
import android.text.TextUtils;
import android.util.Log;

import com.google.gson.Gson;
import com.google.gson.JsonParseException;
import com.google.gson.internal.$Gson$Types;
import com.jshongli.myapplication.common.utils.ToastUtils;
import com.zhy.http.okhttp.callback.Callback;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

import okhttp3.Call;
import okhttp3.Response;

/**
 * Created by Administrator on 2017/6/21.
 */

public abstract class NillCallback<T > extends Callback<T> {

    @Override
    public T parseNetworkResponse(Response response, int id) throws Exception {

        return null;
    }

    @Override
    public void onError(Call call, Exception e, int id) {

        ToastUtils.show(mContext, e.getMessage());
    }

    @Override
    public void onResponse(T response, int id) {


    }

    public NillCallback(Context context) {
        this.mContext = context;
        mType = getSuperclassTypeParameter(getClass());
    }

    private Context mContext;
    public Type mType;

    /**
     * 通过反射想要的返回类型
     *
     * @param subclass
     * @return
     */
    static Type getSuperclassTypeParameter(Class<?> subclass) {
        Type superclass = subclass.getGenericSuperclass();
        if (superclass instanceof Class) {
            throw new RuntimeException("Missing type parameter.");
        }
        ParameterizedType parameterized = (ParameterizedType) superclass;
        return $Gson$Types.canonicalize(parameterized.getActualTypeArguments()[0]);
    }


}
